#include "EquipMergeScene.h"
#include "../../../utils/LocalString.h"
#include "../../../model/proxy/ItemProxy.h"
#include "../NotifyDefine.h"
#include "../../../net/NetController.h"
#include "../dialog/SelectItemDialog.h"
#include "../../../model/proxy/UserProxy.h"
#include "../../../model/static/StaticItem.h"

EquipMergeScene::EquipMergeScene(void)
	:mName(NULL),mType(NULL),mLevel(NULL),mPerfect(NULL),mAddProperty(NULL),mQiangHuaCost(NULL),
	mNeedEnergy(NULL),mStoveEnergy(NULL),mPropertyTitle(NULL),mProperty(NULL),
	mEquipBtn1(NULL),mEquipBtn2(NULL),mEquipBtn3(NULL),mEquipBtn4(NULL)
{
}

EquipMergeScene::~EquipMergeScene(void)
{
	CC_SAFE_RELEASE(mName);
	CC_SAFE_RELEASE(mType);
	CC_SAFE_RELEASE(mLevel);
	CC_SAFE_RELEASE(mPropertyTitle);
	CC_SAFE_RELEASE(mProperty);
	CC_SAFE_RELEASE(mPerfect);
	CC_SAFE_RELEASE(mAddProperty);
	CC_SAFE_RELEASE(mQiangHuaCost);
	CC_SAFE_RELEASE(mNeedEnergy);
	CC_SAFE_RELEASE(mStoveEnergy);
	CC_SAFE_RELEASE(mEquipBtn1);
	CC_SAFE_RELEASE(mEquipBtn2);
	CC_SAFE_RELEASE(mEquipBtn3);
	CC_SAFE_RELEASE(mEquipBtn4);
}

bool EquipMergeScene::init()
{
	return true;
}

SEL_MenuHandler EquipMergeScene::onResolveCCBCCMenuItemSelector(CCObject * pTarget, CCString * pSelectorName )
{
	CCB_SELECTORRESOLVER_CCMENUITEM_GLUE(this, "onEquipBgClick", EquipMergeScene::onEquipBgClick);
	return NULL;
}

SEL_CCControlHandler EquipMergeScene::onResolveCCBCCControlSelector( CCObject * pTarget, CCString * pSelectorName )
{
	CCB_SELECTORRESOLVER_CCCONTROL_GLUE(this, "onQiangHuaBtnClick", EquipMergeScene::onQiangHuaBtnClick);
	CCB_SELECTORRESOLVER_CCCONTROL_GLUE(this, "onXiLianBtnClick", EquipMergeScene::onXiLianBtnClick);
	CCB_SELECTORRESOLVER_CCCONTROL_GLUE(this, "onSupplyBtnClick", EquipMergeScene::onSupplyBtnClick);
	CCB_SELECTORRESOLVER_CCCONTROL_GLUE(this, "onChangeEquipBtnClick", EquipMergeScene::onChangeEquipBtnClick);
	return NULL;
}

bool EquipMergeScene::onAssignCCBMemberVariable( CCObject * pTarget, CCString * pMemberVariableName, CCNode * pNode )
{
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mName", CCLabelTTF *, this->mName);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mType", CCLabelTTF *, this->mType);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mLevel", CCLabelTTF *, this->mLevel);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mPropertyTitle", CCLabelTTF *, this->mPropertyTitle);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mProperty", CCLabelTTF *, this->mProperty);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mPerfect", CCLabelTTF *, this->mPerfect);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mAddProperty", CCLabelTTF *, this->mAddProperty);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mQiangHuaCost", CCLabelTTF *, this->mQiangHuaCost);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mNeedEnergy", CCLabelTTF *, this->mNeedEnergy);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mStoveEnergy", CCLabelTTF *, this->mStoveEnergy);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mEquipBtn1", CCMenuItemImage *, this->mEquipBtn1);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mEquipBtn2", CCMenuItemImage *, this->mEquipBtn2);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mEquipBtn3", CCMenuItemImage *, this->mEquipBtn3);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mEquipBtn4", CCMenuItemImage *, this->mEquipBtn4);
	return false;
}

void EquipMergeScene::onNodeLoaded( CCNode * pNode, CCNodeLoader * pNodeLoader )
{
    CCMenuItemImage* menuItemList[] = {mEquipBtn1,mEquipBtn2,mEquipBtn3,mEquipBtn4};
	for (int i = 0; i < 4; i++)
	{
		MergeItem* mergeItem = new MergeItem();
		mergeItem->mMenuItem = menuItemList[i];
		mMergeItemList.push_back(mergeItem);
	}

	mEquipIcon = EquipIcon::create(this);
	mEquipIcon->setPosition(ccp(23, 357));
	mEquipIcon->setScale(0.4);
	addChild(mEquipIcon);
}

void EquipMergeScene::onEnter()
{
	State::onEnter();
	CCArray* nameList = CCArray::create(ccs(kVCSelectItem),ccs(kNCIntensifyEquipage),ccs(kNCFusionEquipage),ccs(kVCBuyZhuRongJi),NULL);
	RegisterObservers(this, nameList, callfuncO_selector(EquipMergeScene::_onNotification));
	refresh();
}

void EquipMergeScene::onExit()
{
	State::onExit();
}

void EquipMergeScene::_onNotification( CCObject* object )
{
	CCLog("EquipMergeScene::_onNotification[%x]", this);

	NotificationObserver* notification = (NotificationObserver*)object;
	std::string name = std::string(notification->getName());
	if (name == kVCSelectItem)
	{
		if (isChangeEquip)
		{
			int select = ItemProxy::getSingletonPtr()->mSelectList[0];
			ItemProxy::getSingletonPtr()->setLastQiangHuaEquip(ItemProxy::getSingletonPtr()->getEquip(select)) ;
			refresh();
		} 
		else
		{
			Framework::getSingletonPtr()->popup("MeltOkDialog", NULL);
		}
	}
	else if(name == kNCIntensifyEquipage)
	{
		Framework::getSingletonPtr()->popup("EquipLevelUpDialog", NULL);
	}
	else if(name == kNCFusionEquipage)
	{
		
	}
    else if(name == kNCResetEquipage)
	{
		refresh();
	}
	else if(name == kVCBuyZhuRongJi)
	{
		for (int i = 0; i < mMergeItemList.size(); i++)
		{
			if(mMergeItemList[i]->mItemIcon == NULL)
			{
				mMergeItemList[i]->mItemIcon = new Item();
				mMergeItemList[i]->mItemIcon->itemID = 70001;
				mMergeItemList[i]->mItemIcon->setIcon();
				mMergeItemList[i]->mItemIcon->setPosition(ccp(103+i*60,181));
				addChild(mMergeItemList[i]->mItemIcon);
				break;
			}
		}
	}
}

void EquipMergeScene::refresh()
{
	EquipInfo* equipInfo = ItemProxy::getSingletonPtr()->lastQiangHuaEquip;
	EquipStatic* equipStatic = StaticItem::getSingletonPtr()->getEquipInfo(equipInfo->id);
	mEquipIcon->setInfo(equipInfo->id, equipStatic->quality);
	mName->setString(equipStatic->name);
	mType->setString(StaticItem::getSingletonPtr()->getTypeTitle(equipInfo->id));
	mLevel->setString(itos(equipInfo->level));
	mPropertyTitle->setString(StaticItem::getSingletonPtr()->getPropertyTitle(equipInfo->id));
	string tempStr;
	ItemProxy::getSingletonPtr()->getPropertyValue(equipInfo,tempStr);
	mProperty->setString(tempStr.c_str());
	mPerfect->setString("10% - 100%");
	mAddProperty->setString("2 - 10");
	mNeedEnergy->setString("10");
	mQiangHuaCost->setString("100");
	mStoveEnergy->setString(itos(UserProxy::getSingletonPtr()->userVO.rEnergy));
}

void EquipMergeScene::onEquipBgClick(CCObject* pSender)
{
    int count = 0;
    for (int i = 0; i < mMergeItemList.size(); i++) {
        if(mMergeItemList[i]->mItemIcon != NULL)
            count++;
    }
    if(count < 4)
    {
        for (int i = 0; i < mMergeItemList.size(); i++)
        {
            if (pSender == mMergeItemList[i]->mMenuItem)
            {
                curSelect = i;
                Framework::getSingletonPtr()->popup("BuyZhuRongJiDialog", NULL);
                break;
            }
        }
    }
}

void EquipMergeScene::onQiangHuaBtnClick( CCObject * pSender, CCControlEvent pCCControlEvent )
{
    int count = 0;
    for (int i = 0; i < mMergeItemList.size(); i++)
    {
        if(mMergeItemList[i]->mItemIcon != NULL)
            count++;
    }
    NetController::getSingletonPtr()->intensifyEquipage(ItemProxy::getSingletonPtr()->lastQiangHuaEquip->index,
        count);
}

void EquipMergeScene::onXiLianBtnClick( CCObject * pSender, CCControlEvent pCCControlEvent )
{
	Framework::getSingletonPtr()->popup("XiLianOkDialog", ItemProxy::getSingletonPtr()->lastQiangHuaEquip);
}

void EquipMergeScene::onSupplyBtnClick( CCObject * pSender, CCControlEvent pCCControlEvent )
{
	isChangeEquip = false;
	SelectMode sm = MultiSelect;
	Framework::getSingletonPtr()->popup("SelectItemDialog", &sm);
}

void EquipMergeScene::meltEquip(vector<int>& equipList)
{

}

void EquipMergeScene::onChangeEquipBtnClick( CCObject * pSender, CCControlEvent pCCControlEvent )
{
	isChangeEquip = true;
	SelectMode sm = SingleSelect;
	Framework::getSingletonPtr()->popup("SelectItemDialog", &sm);
}
